package servlet;

import bean.TodoItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/TodoServlet")
public class TodoServlet extends HttpServlet {

    @SuppressWarnings("unchecked")
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        try {
            // 获取表单数据
            String title = request.getParameter("title");
            String itemTitle = request.getParameter("itemTitle");
            String action = request.getParameter("action");

            // 验证输入
            if (title != null && title.trim().isEmpty()) {
                session.setAttribute("error", "待办事项标题不能为空");
                response.sendRedirect("todo.jsp");
                return;
            }

            // 获取或创建待办列表
            List<TodoItem> todoList = (List<TodoItem>) session.getAttribute("todos");
            if (todoList == null) {
                todoList = new ArrayList<>();
            }

            // 根据操作类型执行不同逻辑
            if ("add".equals(action)) {
                // 检查是否已存在相同标题的待办事项
                boolean exists = todoList.stream()
                        .anyMatch(item -> item.getTitle().equals(title));
                if (exists) {
                    session.setAttribute("error", "该待办事项已存在");
                } else {
                    todoList.add(new TodoItem(title, "未完成"));
                    session.setAttribute("message", "添加成功");
                }
            } else if ("complete".equals(action)) {
                boolean found = false;
                for (TodoItem item : todoList) {
                    if (item.getTitle().equals(itemTitle)) {
                        item.setStatus("已完成");
                        found = true;
                        session.setAttribute("message", "任务已完成");
                        break;
                    }
                }
                if (!found) {
                    session.setAttribute("error", "未找到指定的待办事项");
                }
            } else if ("delete".equals(action)) {
                boolean removed = todoList.removeIf(item -> item.getTitle().equals(itemTitle));
                if (removed) {
                    session.setAttribute("message", "删除成功");
                } else {
                    session.setAttribute("error", "未找到指定的待办事项");
                }
            }

            session.setAttribute("todos", todoList);
        } catch (Exception e) {
            session.setAttribute("error", "操作失败：" + e.getMessage());
        }

        response.sendRedirect("todo.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}